package com.adee.java;

import java.util.concurrent.*;

public class Test028_BlockingQueue {
    public static void main(String[] args) {
        test();
    }

    /*
    *
    * */
    public static void test() {
        BlockingQueue<Integer> q = new LinkedBlockingQueue<>(10);
        ExecutorService in = Executors.newFixedThreadPool(1);
        ExecutorService out = Executors.newFixedThreadPool(1);
        in.submit(() -> {
            for(;;) {
                try {
                    TimeUnit.MILLISECONDS.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                int i = ThreadLocalRandom.current().nextInt(100);
                System.out.println("加入 " + i);
                q.add(i);
            }
        });
        out.submit(() -> {
            for(;;) {
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("取出 " + q.remove());
            }
        });
    }
}
